package com.gamefx.engine1.managers
{
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import com.gamefx.engine1.managers.soundClasses.*;
	

	public class SoundManagerImpl implements ISoundManager
	{
		private static var instance:SoundManagerImpl;

		private var soundBoxs:Dictionary;
		private var channelBoxs:Object;
		private var accumulator:uint;
		private var count:uint;

		public function SoundManagerImpl()
		{
			super();

			if (instance != null)
			{
				throw new Error("Instance already exists.");
			}

			instance = this;

			soundBoxs = new Dictionary(true);
			channelBoxs = {};
		}

		public static function getInstance():SoundManagerImpl
		{
			if (!instance)
			{
				instance = new SoundManagerImpl();
			}

			return instance;
		}

		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  soundMuted
		//----------------------------------
		
		private var originalVolume:Number;
		private var _soundMuted:Boolean;

		public function get soundMuted():Boolean
		{
			return _soundMuted;
		}

		public function set soundMuted(value:Boolean):void
		{
			_soundMuted = value;
			
			var transform:SoundTransform = SoundMixer.soundTransform;
			
			if (_soundMuted)
			{
				originalVolume = transform.volume;
				transform.volume = 0;
			}
			else
			{
				transform.volume = originalVolume;
			}
			SoundMixer.soundTransform = transform;
		}
		
		//----------------------------------
		//  soundVolume
		//----------------------------------
		
		public function get soundVolume():Number
		{
			var transform:SoundTransform = SoundMixer.soundTransform;
			return transform.volume;
		}

		public function set soundVolume(value:Number):void
		{
			originalVolume = value;
			
			if (!_soundMuted)
			{
				var transform:SoundTransform = SoundMixer.soundTransform;
				transform.volume = value;
				SoundMixer.soundTransform = transform;
			}
		}
		
		

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function play(url:String, starTime:Number = 0, loops:uint = 0):uint
		{
			var soundBox:SoundBox;

			if (soundBoxs.hasOwnProperty(url))
			{
				soundBox = soundBoxs[url] as SoundBox;
			}
			else
			{
				soundBox = soundBoxs[url] = new SoundBox(url);
			}

			accumulator++;
			channelBoxs[accumulator] = new ChannelBox(accumulator, soundBox, starTime, loops);
			count++;

			return accumulator;
		}


		public function stop(id:uint):void
		{
			if (channelBoxs.hasOwnProperty(id))
			{
				var channelBox:ChannelBox = channelBoxs[id] as ChannelBox;
				channelBox.stop();
				delete channelBoxs[id];
				count--;

				if (count <= 0)
				{
					accumulator = 0;
				}
			}
		}


		public function destory(url:String, isError:Boolean = false):void
		{
			if (soundBoxs.hasOwnProperty(url))
			{
				var soundBox:SoundBox = soundBoxs[url] as SoundBox;
				soundBox.destory();
				delete soundBoxs[url];
				
				if (isError)
				{
					for (var id:String in channelBoxs)
					{
						if ((channelBoxs[id] as ChannelBox).soundBox === soundBox)
						{
							delete channelBoxs[id];
						}
					}
				}
			}
		}


	}
}